//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include <buola/cv/coctree.h>

using namespace buola;

struct CInt
{
    CInt(int pI)
        :   mI(pI)
    {
        msg_info() << "constructing " << pI << "\n";
    }

    ~CInt()
    {
        msg_info() << "destroying " << mI << "\n";
    }

    int mI;
};

io::CTextWriter &operator<<(io::CTextWriter &pW,const CInt &pInt)
{
    pW << pInt.mI << " ";
    return pW;
}

using namespace buola;

void DumpTree(const cvi::COcTreeDefaultKey &pKey,int pDepth)
{
    for(int i=0;i<pDepth;i++) msg_info() << "  ";
    msg_info() << pKey << "   " << pKey.AncestorAt(1) << "   " << pKey.Coords(1.0,pDepth) << "\n";
    
    if(pDepth==pKey.cMaxDepth) return;

    for(int i=0;i<8;i++)
        DumpTree(pKey.Child(i,pDepth),pDepth+1);
}

mat::CVec3f SumTree(const cvi::COcTreeDefaultKey &pKey,int pDepth)
{
    mat::CVec3f lVector=pKey.Coords(1.0f,pDepth);

    if(pDepth==pKey.cMaxDepth) return lVector;

    for(int i=0;i<8;i++)
        lVector+=SumTree(pKey.Child(i,pDepth),pDepth+1);

    return lVector;
}

int main()
{
    cvi::COcTree<double> lTree(10.0);

    start_timer();
    lTree.Expand();
    end_timer();

    msg_info() << lTree.size() << "\n";

    {
        start_timer();
        int lAccDepth=0;

        for(auto &i : lTree)
            lAccDepth+=1+i.Value();
        end_timer();
        msg_info() << lAccDepth << "\n";
    }
    {
        start_timer();
        int lAccDepth=0;

        for(auto i=lTree.VoxelBegin();i!=lTree.VoxelEnd();++i)
            lAccDepth+=1;//i->Value();
        end_timer();
        msg_info() << lAccDepth << "\n";
    }
    msg_info() << sizeof(mat::CVec3f) << "\n";
    {
        start_timer();
        mat::CVec3f lSum({0,0,0});

        for(auto i=lTree.VoxelBegin();i!=lTree.VoxelEnd();++i)
        {
            lSum+=i.Coords();
        }
        end_timer();
        msg_info() << lSum << "\n";
    }

    std::vector<cvi::COcTreeDefaultKey> lRay=lTree.ComputeRayKeys({-13.3,1.2,5.1},{17.2,19.4,-23.6});

    for(auto &k : lRay)
        msg_info() << k.Coords(10.0) << "\n";
}